#include "diagnosis_menu.h"

Diagnosis_Menu::Diagnosis_Menu(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Init();
    connect_Init();
}

void Diagnosis_Menu::ui_Init()
{

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
    {
         theLabel[i] = new QLabel(this);
    }
    theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[1]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
   theLabel[0]->setGeometry(0,0,150,60);//设定每隔放置一个按键
   theLabel[1]->setGeometry(150,0,490,60);//设定每隔放置一个按键
   theLabel[1]->setText("仪器设置           ");

    for(int i=0;i<8;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
        theBtn[i]->setFixedSize(120,90);
        btnlist_ui.append(theBtn[i]);

}
btnlist_ui.at(0)->setText(tr("软件版本"));
btnlist_ui.at(1)->setText(tr("电压"));
btnlist_ui.at(2)->setText(tr("流量"));
btnlist_ui.at(3)->setText(tr("温度"));
btnlist_ui.at(4)->setText(tr("电磁阀"));
btnlist_ui.at(5)->setText(tr("仪器配置"));
btnlist_ui.at(6)->setText(tr("压力"));
btnlist_ui.at(7)->setText(tr("返回"));

theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(10, 150, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/软件版本.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(10, 150, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/软件版本2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(170, 150, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/电压.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(170, 150, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/电压2.png); /* 背景图片 */"+"}");
theBtn[2]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(230, 210, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/流量.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(230, 210, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/流量2.png); /* 背景图片 */"+"}");
theBtn[3]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(220, 50, 5);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/温度.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(220, 50, 5);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/温度2.png); /* 背景图片 */"+"}");
theBtn[4]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(60, 255, 15);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/电磁阀.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"background-color: rgb(60, 255, 15);	    /* 背景颜色 */ ;"+"color: rgb(255,125,25);	  /* 文本颜色 */"+ "background-image: url(:/pic/Diagnosis/电磁阀2.png); /* 背景图片 */"+"}");
theBtn[5]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(170, 15, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/仪器配置.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(170, 15, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/仪器配置2.png); /* 背景图片 */"+"}");
theBtn[6]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(230, 20, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/压力.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(230, 20, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/Diagnosis/压力2.png); /* 背景图片 */"+"}");
theBtn[7]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(220, 50, 105);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(220, 50, 105);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");


QGridLayout *mainLayout = new QGridLayout();
QGridLayout *mainLayout2 = new QGridLayout();
mainLayout->addWidget(btnlist_ui.at(0), 1, 0);
mainLayout->addWidget(btnlist_ui.at(1), 1, 1);
mainLayout->addWidget(btnlist_ui.at(2), 1, 2);
mainLayout->addWidget(btnlist_ui.at(3), 1, 3);
//mainLayout->setVerticalSpacing(50);
//mainLayout->setHorizontalSpacing(10);
mainLayout->setContentsMargins(0,120,0,0);//(int left, int top, int right, int bottom)
//mainLayout->setColumnStretch(1,22);
mainLayout->setSizeConstraint(QLayout::SetMinimumSize);//布局显示效果
  setLayout(mainLayout);
  mainLayout2->addWidget(btnlist_ui.at(4), 1, 0);
  mainLayout2->addWidget(btnlist_ui.at(5), 1, 1);
  mainLayout2->addWidget(btnlist_ui.at(6), 1, 2);
  mainLayout2->addWidget(btnlist_ui.at(7), 1, 3);
//  mainLayout2->setVerticalSpacing(50);
//  mainLayout2->setHorizontalSpacing(20);
  mainLayout2->setContentsMargins(0,50,0,0);//(int left, int top, int right, int bottom)
  mainLayout2->setSizeConstraint(QLayout::SetMinimumSize);
    setLayout(mainLayout2);
  QWidget* widget = new QWidget(this);
  QWidget* widget2 = new QWidget(this);
  widget->setGeometry(0,0,640,200);
  widget2->setGeometry(0,200,640,200);
  widget->setLayout(mainLayout);
  widget2->setLayout(mainLayout2);

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}
void Diagnosis_Menu::connect_Init()
{
    //版本信息
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        Version  *menux = new Version(this);
        menux->show();

    });

    //电压
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        port_Voltage  *menux = new port_Voltage(this);
        menux->show();
    });

    //流量
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){
        Flow_Show  *menux = new Flow_Show(this);
        menux->show();
    });
    //温度
    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
       temperature_Show  *menux = new temperature_Show(this);
        menux->show();
    });
    //电磁阀
    connect(btnlist_ui.at(4),&QPushButton::clicked,[=](){
       Elec_Show  *menux = new Elec_Show(this);
        menux->show();
    });
    //维护模式
    connect(btnlist_ui.at(5),&QPushButton::clicked,[=](){
       Insetrument_Set  *menux = new Insetrument_Set(this);
        menux->show();
    });
    //压力
    connect(btnlist_ui.at(6),&QPushButton::clicked,[=](){
        Pressure  *set = new Pressure(this);
        set->show();
    });
    //返回
    connect(btnlist_ui.at(7),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });
}
bool Diagnosis_Menu::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
        for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(i) == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case OK:
                 {
                   emit  button->clicked();
                   break;
                 }
               case UP://上
               {
                   if(focusWidget() != btnlist_ui.first())
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   break;
               }
               case DOWN://下
               {
                   if(focusWidget() != btnlist_ui.back())
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   break;
               }
               case LEFT://左
               {
                 if(focusWidget() != btnlist_ui.first())
                      focusPreviousChild();
                 break;
               }
               case RIGHT://右
               {
                 if(focusWidget() != btnlist_ui.back())
                      focusNextChild();
              break;
               }
               case BACK:
               {
                   this->parentWidget()->show();
                   this->close();
                   break;
               }
               case INIT:
               {

                   init_Show *show = new init_Show(this);
                   show->show();
                   this->hide();
                   break;
               }
               default:
                   break;
               }
            }
            else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
                      {
                          QPushButton *focus_button = (QPushButton *)watched;
                                  focus_button->setFixedSize(160,120);
//                                  focus_button->setStyleSheet(focusBtn[2]->styleSheet());
//                                  focus_button->setIconSize(QSize(1,2));
                      }
            else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
                     {
                QPushButton *focus_button = (QPushButton *)watched;
                        focus_button->setFixedSize(120,90);
                     }
            }
        return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}
